﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoRule
{
    class CAvgItem
    {
        public String devtype;
        public int    count = 0;
        public double sum = 0.0;
        public double avg = 0.0;
    }
    
    class CAvg
    {
        public void reset()
        {
            _list = new List<CAvgItem>();
        }

        public void sum(String devtype, double value)
        {
            bool bFound = false;
            foreach (CAvgItem item in _list)
            {
                if (item.devtype.Equals(devtype))
                {
                    item.sum += value;
                    ++item.count;
                    bFound = true;
                    break;
                }
            }

            if (!bFound)
            {
                CAvgItem item = new CAvgItem();
                item.devtype = devtype;
                item.sum += value;
                ++item.count;
                _list.Add(item);
            }
        }

        public void doavg()
        {
            foreach (CAvgItem item in _list)
            {
                item.avg = item.sum / item.count;
            }
        }

        public double getavg(String devtype)
        {
            double avg = 0.0;
            foreach (CAvgItem item in _list)
            {
                if (item.devtype.Equals(devtype))
                {
                    avg = item.avg;
                    break;
                }
            }
            return avg;
        }

        private List<CAvgItem> _list = null;
    }
}
